$(function() {
	//初始化datatable
	$("#table").XRDataTable({
		ajaxUrl: "Admin/Communication/getDatas",
		paramFunc: paramFunc,
		operation: {
			"edit": ["Admin/Communication/edit/#id", "编辑"],
			"icon-trash": [deleteModule, "删除"],
			"icon-ok": [changeStatus, "提交审核"],
			// "icon-check": [changeSubmitKh, "提交考核库"],
			"icon-legal":['Admin/Communication/auditView/#id',"审核进程"]	
		},
		hasCheckbox: false,
		afterFunc:afterFunc
	});

	$('.cart-c-title-icon').click(function() {
        	$('.cart-c').animate({ right: "-400px" }, function() {
        });
    });
    resetCartPosition();
    $(window).resize(function(){
    	resetCartPosition();
    });
});

var paramFunc = function() {
	var data = getFormJson();
	return data;
};

var afterFunc = function(){
	// alert(JSON.stringify(trData));
	$("#table tbody tr").each(function() {
		var data = $("#table").getOpts().datas[$(this).index()];
		if(data.is_audit)
		{
			$(this).find('#icon-ok').show();
			$(this).find('#icon-trash').show();
		}
		else{
			$(this).find('#icon-ok').hide();
			$(this).find('#icon-trash').hide();
		}
		if(data.status==$("#unSubmit").val())
		{
			$(this).find('#icon-legal').hide();
		}
		if (data.is_edit) {
			$(this).find('#fa-edit').show();
		}
		else {
			$(this).find('#fa-edit').hide();
		}
		
		if(data.change_status=='99'){
			$(this).addClass('tr-red');
		}else if(data.change_status=='3'){
			$(this).addClass('tr-green');
		}

		if(data.is_submit_kh == 0){
			$(this).find('#icon-check').show();
		}else{
			$(this).find('#icon-check').hide();
		}
	});
	//二维码放大
	imgBig('erweima img');
}

function resetCartPosition() {
    $('.cart-c').height(Number($(window).height()) - 55);
    $('.cart-c-box').height(Number($(window).height()) - 105);
}


function exportData(){
	var load_alert=loadAlert('数据统计中...');
	$.ajax({
		type: "post",
		url: "Admin/Communication/getDatas/isExport/true",
		async: true,
		dataType:'json',
		data:$('#searchForm').serialize(),
		success: function(data) {
			closeAlert(load_alert);
			if(data.status) {
	            window.location.href=data.v;
			} else {
				window.parent.topMessagePrompt('error',data.msg);
			}
		}
	});
}

var deleteModule=function(idx, trData){
	var msg = "是否确认删除【" + trData.name + "】?";
	swal({
		title: "请确认",
		text: msg,
		type: "warning",
		showCancelButton: true,
		closeOnConfirm: true,
		confirmButtonText: "确定",
		cancelButtonText: '取消',
		confirmButtonColor: "#ec6c62"
	}, function(isConfirm) {
		if(isConfirm) {
			$.ajax({
				type: "post",
				url: "Admin/Communication/delete/"+trData.id,
				async: true,
				success: function(data) {
					var ret = $.parseJSON(data);
					if(ret.status) {
						window.parent.topMessagePrompt('success',ret.msg);
	                    window.parent.refreshTable();
					} else {
						window.parent.topMessagePrompt('error',ret.msg);
					}
				}
			});
		}
	});
}

var changeStatus=function(idx, trData){
	var msg = "是否提交审核?";
	swal({
		title: "请确认",
		text: msg,
		type: "warning",
		showCancelButton: true,
		closeOnConfirm: true,
		confirmButtonText: "确定",
		cancelButtonText: '取消',
		confirmButtonColor: "#ec6c62"
	}, function(isConfirm) {
		if(isConfirm) {
			$.ajax({
				type: "post",
				url: "Admin/Communication/submit_audit/"+trData.id,
				async: true,
				success: function(data) {
					var ret = $.parseJSON(data);
					if(ret.status) {
						window.parent.topMessagePrompt('success',ret.msg);
	                    window.parent.refreshTable();
					} else {
						window.parent.topMessagePrompt('error',ret.msg);
					}
				}
			});
		}
	});
}

//是否提交考核库
// var changeSubmitKh = function(idx,trData){
// 	var msg = "考核只能提交一次,是否需要提交考核库?";
// 	swal({
// 		title: "请确认",
// 		text: msg,
// 		type: "warning",
// 		showCancelButton: true,
// 		closeOnConfirm: true,
// 		confirmButtonText: "确定",
// 		cancelButtonText: '取消',
// 		confirmButtonColor: "#ec6c62"
// 	}, function (isConfirm) {
// 		if (isConfirm) {
// 			$.ajax({
// 				type: "post",
// 				url: "Admin/Communication/submit_kh/" + trData.id,
// 				async: true,
// 				success: function (data) {
// 					var ret = $.parseJSON(data);
// 					if (ret.status) {
// 						window.parent.topMessagePrompt('success', ret.msg);
// 						window.parent.refreshTable();
// 					} else {
// 						window.parent.topMessagePrompt('error', ret.msg);
// 					}
// 				}
// 			});
// 		}
// 	});
// }